<template>
  <div class="bg">
    <div class="login">
      <el-form label-width="=50px" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <el-form-item label="账号" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handerLogin">立即登录</el-button>
          <el-button @click="handerResetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginFormRules: {
        // 用户名是否合法
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 5, max: 16, message: "长度只能在5-16位之间", trigger: "blur" },
        ],
        // 密码
        password: [
          { required: true, message: "请您输入密码", trigger: "blur" },
          { min: 6, max: 16, message: "长度只能在6-16位之间", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // async
    // 登录按钮事件
    handerLogin() {
      // console.log("aaa");
      // post参数：是直接携带
      // get参数：parmas
      // const { data: data } = await this.$http.post("login", this.loginForm);
      // console.log(data);

      // console.log(this);
      // console.log(this.$refs.loginFormRef.validate());
      this.$refs.loginFormRef.validate(async (valid) => {
        console.log(valid);
        if (!valid) return;
        const { data: data } = await this.$http.post("login", this.loginForm);
        console.log(data);
        if (data.meta.status !== 200) return this.$message.error(data.meta.msg);
        this.$message.success(data.meta.msg);
        /* 
          1.讲token存放在sessionStorage里面，这样子安全一些，浏览器关闭就会自动清除token

        */

        // console.log(data.data.token);
        window.sessionStorage.setItem("token", data.data.token);

        // 跳转到home页面
        this.$router.push("/home");
      });
    },
    // 重置按钮事件
    handerResetLoginForm() {
      // this.loginForm.username = "";
      // this.loginForm.password = "";
      // console.log(this.$refs.loginFormRef);
      this.$refs.loginFormRef.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.bg {
  width: 100%;
  height: 100vh;
  background: url("../../../public/img/bg.jpg") no-repeat center;
  background-size: cover;
  position: relative;
  .login {
    width: 35%;
    height: 400px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 100px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    align-items: center;
    justify-content: center;
    .el-form {
      width: 400px;
      .el-button {
        width: 48%;
      }
    }
    // /deep/这是深度的意思，凡是样式初始化修改不了，就要去使用这个
    /deep/ .el-form-item__label {
      font-size: 20px;
      font-weight: bold;
    }
  }
}
</style>
